<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>欢迎页面-X-admin2.2</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="../../static/css/font.css">
        <link rel="stylesheet" href="../../static/css/xadmin.css">
        <script src="../../static/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="../../static/js/xadmin.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="x-nav">
          <span class="layui-breadcrumb">
            <a href="#">首页</a>
            <a href="#">模版管理</a>
            <a>
              <cite>模版列表</cite></a>
          </span>
          <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
        </div>
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <form class="layui-form layui-col-space5">
                                <div class="layui-inline layui-show-xs-block">
                                    <input class="layui-input"  autocomplete="off" placeholder="开始日" name="start" id="start">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input class="layui-input"  autocomplete="off" placeholder="截止日" name="end" id="end">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <input type="text" name="username"  placeholder="请输入用户名" autocomplete="off" class="layui-input">
                                </div>
                                <div class="layui-inline layui-show-xs-block">
                                    <button class="layui-btn"  lay-submit="" lay-filter="sreach"><i class="layui-icon">&#xe615;</i></button>
                                </div>
                            </form>
                        </div>
                        <div class="layui-card-header">
                            <button class="layui-btn" onclick="xadmin.open('添加模版','/TemplateAdd/',400,200)"><i class="layui-icon"></i>添加</button>
                        </div>
                        <div class="layui-card-body " id="template" lay-filter="template">

                        </div>
                    </div>
                </div>
            </div>
        </div> 
    </body>
    <script>
      layui.use(['laydate', 'form', 'table'], function() {
          let laydate = layui.laydate;
          let form = layui.form;
          let table = layui.table;
          //执行一个laydate实例
          laydate.render({
              elem: '#start' //指定元素
          });

          //执行一个laydate实例
          laydate.render({
              elem: '#end' //指定元素
          });
          table.render({
              elem: '#template'
              , height: 600
              , url: '/Template/' //数据接口
              , method: 'post'  //
              , page: true //开启分页
              , cols: [[ //表头
                  {checkbox: true}
                  , {field: 'id', title: 'ID'}
                  , {field: 'name', title: '模版名称'}
                  , {field: 'create_time', title: '创建时间'}
                  , {title: '操作', align: 'center', toolbar: '#bar'}
              ]]
          });

          table.on('tool(template)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
              let data = obj.data; //获得当前行数据
              let layEvent = obj.event; //获得 lay-event 对应的值
              let tr = obj.tr; //获得当前行 tr 的DOM对象

              if (layEvent === 'del') { //删除
                  layer.confirm('真的删除行么', function (index) {
                      obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                      layer.close(index);
                      //向服务端发送删除指令
                      $.post(
                          "/TemplateDel/",
                          data,
                          function (res) {
                              layer.msg(res.message, {time: 2000});
                          },
                          'json'
                      );
                  });
              } else if (layEvent === 'edit') { //编辑
                  //do something
                  let htmlData = '        <div class="layui-fluid">\n' +
                      '            <div class="layui-row">\n' +
                      '                <form class="layui-form">\n' +
                      '                  <div class="layui-form-item">\n' +
                      '                      <label for="name" class="layui-form-label">\n' +
                      '                          <span class="x-red">*</span>模版名称\n' +
                      '                      </label>\n' +
                      '                      <div class="layui-input-inline">\n' +
                      '                          <input type="text" id="name" name="name" required="" lay-verify="required"\n' +
                      '                          autocomplete="off" class="layui-input" value="' + data.name + '">\n' +
                      '                      </div>\n' +
                      '                  </div>\n' +
                      '              </form>\n' +
                      '            </div>\n' +
                      '        </div>';
                  layer.open({
                      title: "修改名称",
                      area: ["400px", "200px"],
                      type: 1,
                      content: htmlData,//把数据显示在弹出层上
                      btn: ['确定', '我再想想'],
                      yes: function (index, layero) {//改完，点击确定
                          let rename = layero.find("input").val();//获得修改后的值
                          //同步更新缓存对应的值
                          $.post(
                              "/TemplateUpdate/",
                              {name: data.name, rename: rename},
                              function (res) {
                                  layer.msg(res.message, {time: 2000});
                                  if (res.code === 0) {
                                      obj.update({
                                        name: rename

                                      });
                                      layer.close(index);
                                  }
                              },
                              'json'
                          );

                      }
                  });
              }
          });
      });

      function delAll (argument) {

        let data = tableCheck.getData();
  
        layer.confirm('确认要删除吗？'+data,function(index){
            //捉到所有被选中的，发异步进行删除
            layer.msg('删除成功', {icon: 1});
            $(".layui-form-checked").not('.header').parents('tr').remove();
        });
      }
    </script>
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

    </script>
</html>